package com.kefu.service.impl;

import com.kefu.common.CustomizeException;
import com.kefu.model.Kefu;
import com.kefu.service.KefuService;
import com.kefu.mapper.KefuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * Created by Administrator on 2016/11/25.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service("kefuService")
public class KefuServiceImpl implements KefuService {
    @Autowired
    private KefuMapper kefuMapper;
    @Override
    public Kefu selectByNameAndPassword(String username, String password) {
        if (StringUtils.isEmpty(username)) {
            throw new CustomizeException("用户名不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            throw new CustomizeException("密码不能为空");
        }
        //先判断是否存在此username,再判断密码是否正确
         Kefu kefu = kefuMapper.selectByName(username);
        if (StringUtils.isEmpty(kefu)) {
            throw new CustomizeException("不存在此用户");
        } else if (!kefu.getPassword().equals(password)) {
            throw new CustomizeException("密码错误！");
        } else {
            return kefu;
        }
    }

    @Override
    public void addKefu(Kefu kefu) {
        if (StringUtils.isEmpty(kefu.getUsername())) {
            throw new CustomizeException("用户名不能为空");
        }
        if (StringUtils.isEmpty(kefu.getPassword())) {
            throw new CustomizeException("密码不能为空");
        }
        if (StringUtils.isEmpty(kefu.getFullname())) {
            throw new CustomizeException("昵称不能为空");
        }
        //判断是否存在此用户名
        String isUsermane = kefu.getUsername();
        String password = kefu.getPassword();
        Kefu kefu1 = kefuMapper.selectByName(isUsermane);
        if (!StringUtils.isEmpty(kefu1)) {
            throw new CustomizeException("已存在此用户名");
        } else if (isUsermane.getBytes().length == isUsermane.length()) {
            throw new CustomizeException("用户名不能含有汉字");
        } else if (isUsermane.length() < 2 || isUsermane.length() > 16) {
            throw new CustomizeException("用户名不能小于2或者大于16位");
        } else if (password.getBytes().length == password.length()) {
            throw new CustomizeException("密码不能含有汉字");
        } else if (isUsermane.length() < 2 || isUsermane.length() > 16) {
            throw new CustomizeException("密码不能小于2位或者大于16位");
        } else {
            kefuMapper.addKefu(kefu);
        }
    }

    @Override
    public void updateById(Kefu kefu) {
        if (kefu!=null){
            if (StringUtils.isEmpty(kefu.getId())){
                kefu.setId(UUID.randomUUID().toString());
            }
            if (StringUtils.isEmpty(kefu.getUsername())){
                throw new CustomizeException("username不能为空");
            }
            if (StringUtils.isEmpty(kefu.getPassword())){
                throw new CustomizeException("password不能为空");
            }
            if (StringUtils.isEmpty(kefu.getFullname())){
                throw new CustomizeException("fullname不能为空");
            }
            if (StringUtils.isEmpty(kefu.getDocId())){
                throw new CustomizeException("docId不能为空");
            }
            kefuMapper.updateById(kefu);
        }else {
            throw new CustomizeException("kefu对象不能为空");
        }
    }

    @Override
    public Kefu findById(String id) {
        return kefuMapper.findById(id);
    }

    @Override
    public Kefu findByDocId(String docId) {
        return kefuMapper.findByDocId(docId);
    }
}


